package com.cjm.one.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjm.one.goods.mapper.CouponInfoMapper;
import com.cjm.one.goods.mapper.CouponRangeMapper;
import com.cjm.one.goods.service.CouponInfoService;
import com.cjm.one.model.coupon.dto.CouponInfoDto;
import com.cjm.one.model.coupon.entity.CouponInfo;
import com.cjm.one.model.coupon.entity.CouponRange;
import com.cjm.one.model.coupon.vo.CouponInfoVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 29289
* @description 针对表【coupon_info(优惠券信息)】的数据库操作Service实现
* @createDate 2024-09-05 15:55:45
*/
@Service
@Slf4j
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo>
    implements CouponInfoService {
    @Resource
    private CouponInfoMapper couponInfoMapper;
    @Resource
    private CouponRangeMapper rangeMapper;


    @Override
    public Page<CouponInfo> getCouponInfoPage(Long current, Long size, Integer rangeType, String couponName, Integer status) {
        Page<CouponInfo> page = new Page<>(current, size);
        LambdaQueryWrapper<CouponInfo> couponInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (rangeType != null) {
            couponInfoLambdaQueryWrapper.eq(CouponInfo::getRangeType, rangeType);
        }
        if (status != null) {
            couponInfoLambdaQueryWrapper.eq(CouponInfo::getStatus, status);
        }
        log.info("couponName:{}", couponName);
        if (StringUtils.hasText(couponName)) {
            log.info("couponName:{}", couponName);
            couponInfoLambdaQueryWrapper.like(CouponInfo::getName, couponName);
        }
        couponInfoLambdaQueryWrapper.orderByDesc(CouponInfo::getCreateTime);
        Page<CouponInfo> couponInfoPage = couponInfoMapper.selectPage(page, couponInfoLambdaQueryWrapper);
        return couponInfoPage;
    }

    @Override
    public Boolean SaveCoupon(CouponInfoDto couponInfoDto) {
    //TODO 事务
        CouponInfo couponInfo = new CouponInfo();
        BeanUtils.copyProperties(couponInfoDto, couponInfo);
        int insert = couponInfoMapper.insert(couponInfo);
        if (insert > 0) {
            List<CouponRange> rangeList = couponInfoDto.getRangeList();
            rangeList
            .forEach(range -> {
                range.setCouponId(couponInfo.getId());
            });
            rangeMapper.insert(rangeList);
            return true;
        }
        return false;
    }

    @Override
    public Boolean UpdateCoupon(CouponInfoDto couponInfoDto) {
        CouponInfo couponInfo = new CouponInfo();
        BeanUtils.copyProperties(couponInfoDto, couponInfo);
        int update = couponInfoMapper.updateById(couponInfo);
        if (update > 0
                && couponInfoDto.getRangeList() != null
                && !couponInfoDto.getRangeList().isEmpty()){
            rangeMapper.delete(new LambdaQueryWrapper<CouponRange>().eq(CouponRange::getCouponId, couponInfoDto.getId()));
            List<CouponRange> rangeList = couponInfoDto.getRangeList();
            rangeList.forEach(range -> {
                range.setId(null);
                range.setCouponId(couponInfoDto.getId());
            });
            List<BatchResult> insert = rangeMapper.insert(rangeList);
            return insert.size() == rangeList.size();
        }

        return update > 0;
    }

    @Override
    public Boolean DeleteCoupon(Long id) {
        int delete = rangeMapper.delete(new LambdaQueryWrapper<CouponRange>().eq(CouponRange::getCouponId, id));
        int i = couponInfoMapper.deleteById(id);

        return delete > 0 && i > 0;
    }

    @Override
    public CouponInfoVo getCouponById(Long id) {
        CouponInfoVo couponInfoVo = new CouponInfoVo();
        CouponInfo couponInfo = couponInfoMapper.selectById(id);
        BeanUtils.copyProperties(couponInfo, couponInfoVo);
        List<CouponRange> rangeList = rangeMapper.selectList(new LambdaQueryWrapper<CouponRange>().eq(CouponRange::getCouponId, id));
        couponInfoVo.setRangeList(rangeList);
        return couponInfoVo;
    }

    @Override
    public Page<CouponInfoVo> getCouponInfoAppPage(Long current, Long size, Integer rangeType, Long customerId) {
        // 创建分页对象
        Page<CouponInfoVo> page = new Page<>(current, size);

        // 调用 Mapper 层的连表查询方法
        Page<CouponInfoVo> pageVo = couponInfoMapper.selectCouponInfoAppPage(page,rangeType, customerId);


        return pageVo;
    }
}




